import datetime

from flask import Blueprint, current_app
from flask_cors import cross_origin
from flask import request
from datetime import datetime
from app.Services.FinanceServices.IncomeServices import IncomeServices
from app.Services.FinanceServices.ExpenseServices import ExpenseServices
from app.Utils.Utils import success_response, error_response

# 1. 创建蓝图对象
income_bp = Blueprint(
    name='income_bp',
    import_name=__name__,
    url_prefix='/inc'
)

inc_services = IncomeServices()


@income_bp.route('/add', methods=['POST'])
def add_income():
    """
    新增收入
    """
    inc_data = request.json
    status, res = inc_services.create_income(inc_data)
    if status:
        return success_response(res)
    else:
        return error_response(res)


@income_bp.route('/statistics', methods=['GET'])
def get_income_statistics():
    """
    获取收入统计指标卡数据
    :return:
    """
    pass


@income_bp.route('/date_inc', methods=['POST'])
def get_income_list():
    """
    获取某日收入明细数据。
    :return:
    """
    inc_date = request.json['date']
    inc_date = datetime.strptime(inc_date, "%Y-%m-%d").date()
    status, res = inc_services.get_user_incomes(user_id=1, start_date=inc_date, end_date=inc_date)
    if status:
        print(res)
        return success_response(res)
    else:
        print(res)
        return error_response(res)


@income_bp.route('/get_all_first_cates', methods=['GET'])
def get_first_cate():
    """
    获取所有的一级分类
    :return:
    """
    status, res = inc_services.get_all_first_cates()
    if status:
        return success_response(res)
    else:
        return error_response(res)


@income_bp.route('/get_second_cate_by_first_cate_id', methods=['POST'])
def get_second_cate_by_first_cate_id():
    """
    获取某一级分类下的所有二级分类
    :return:
    """
    first_cate_id = request.json.get('first_cate_id', None)
    if first_cate_id is None:
        return error_response(message=f"请上传收入一级分类id")
    status, res = inc_services.get_second_cates_by_first_cate(first_cate_id)
    if status:
        current_app.logger.info(f"get_second_cate_by_first_cate_id({first_cate_id})")
        return success_response(res)
    else:
        current_app.logger.error(f"get_second_cate_by_first_cate_id({first_cate_id})")
        return error_response(message=res)


# 更新支出明细
@income_bp.route('/update_by_id', methods=['POST'])
def update_by_id():
    """
    根据id更新收入明细
    :return:
    """
    inc_record = request.json
    print(inc_record)
    id = inc_record.pop('id', None)
    if id is None:
        return error_response(message=f"请上传收入id")
    status, res = inc_services.update_income(id, **inc_record)
    if status:
        return success_response(res)
    else:
        return error_response(res)


@income_bp.route('/del_by_id', methods=['POST'])
def del_by_id():
    """
    根据id删除收入明细
    :return:
    """
    id = request.json.get('record_id', None)
    if id is None:
        return error_response(message=f"请上传收入明细id")
    status, res = inc_services.delete_income(id)
    if status:
        return success_response(res)
    else:
        return error_response(res)


@income_bp.route('/get_card', methods=['POST'])
def get_card():
    """
    收入指标卡-统计数据。
    """
    year = int(request.json.get('date').split('-')[0])
    month = int(request.json.get('date').split('-')[1])
    print(year, month)
    user_id = 1
    status, res = inc_services.get_inc_card(year, month, user_id)
    if status:
        return success_response(res)
    else:
        return error_response(res)


@income_bp.route('/get_inc_structure', methods=['POST'])
def get_inc_structure():
    """
    收入结构
    """
    year = int(request.json.get('date').split('-')[0])
    month = int(request.json.get('date').split('-')[1])
    user_id = 1
    status, res = inc_services.get_inc_struct(year, month, user_id)
    if status:
        return success_response(res)
    else:
        return error_response(res)

